package cn.com.gridinfo.sooc.core.course.directory.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.directory.bean.Directory;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 课程dao
 * Created by yingchunli on 2015/12/30.
 */
public class CourseDirectoryDao extends SoocIDao {

    private String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    private String statusCode;//提示信息
    private List<Directory> directoryList;//目录列表

    public CourseDirectoryDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result != null) {
            Log.d("result:["+result+"]");
            //登录获取节点服务器是没有如下两个字段的
            status = result.get("status").asText();
            statusCode = result.get("statusCode").asText();
            if (!status.equals("0")) {
                Log.d("服务器返回错误信息: "+status+":"+statusCode);
                return;
            }
        } else {
            return;
        }
        switch (requestCode) {
            case CodeConst.COURSE_DIRECOTRY:
                Log.d("课程目录"+result);
                //获取课程目录
                directoryList = JsonUtil.node2pojoList(result.get("data"), Directory.class);
                break;
        }
    }

    /**
     * 获取课程目录
     *
     * @param kcid        课程id
     */
    public void getDirectory(String kcid) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "newCatalog", true);
        Map<String,String> params = new HashMap<>();
        params.put("kcid", kcid);
        getRequestForCode(url,params,CodeConst.COURSE_DIRECOTRY);
    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public List<Directory> getDirectoryList() {
        return directoryList;
    }
}
